package UniqueInfoboxAttributes;

import zclTool.*;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.util.*;
import java.util.Map.*;

class ClassAtt {
	String ibclass;
	List<String> attlist;
	String feiISA;
	String ontology;
	String ontoFather;

	public ClassAtt(String ibclass) {
		this.ibclass = ibclass;
		this.attlist = new ArrayList<String>();
	}
}

class AttForIndex {
	String fullname;
	String attribute;
	String ibclass;
	String[] words;

	public AttForIndex(String fullname, String attribute) {
		this.fullname = fullname;
		this.attribute = attribute;
	}
}

public class RphAttSchemaMapping2DbpFei {

	/**
	 * @param args
	 */
	static HashMap<String, List<AttForIndex>> attributesIndex = new HashMap<String, List<AttForIndex>>();

	public static void main2(String[] args) throws Exception {

	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		indexAllAttribute();
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"quick_dirty_schema"));
		DelimitedReader dr = new DelimitedReader("atts.text.merged-good");
		HashMap<String, ClassAtt> camap = new HashMap<String, ClassAtt>();
		String[] line;
		while ((line = dr.read()) != null) {
			String a = line[1];
			String[] ab = a.split("/");
			String infobox = ab[0].toLowerCase();
			String att = ab[1];
			if (!camap.containsKey(infobox)) {
				ClassAtt ca = new ClassAtt(infobox);
				ca.attlist.add(att);
				camap.put(infobox, ca);
			} else {
				ClassAtt ca = camap.get(infobox);
				ca.attlist.add(att);
			}
		}
		dr.close();

		// local ontology for classes
		dr = new DelimitedReader("infobox_class_parent");
		while ((line = dr.read()) != null) {
			if (line.length < 5)
				continue;
			String ibclass = line[0].replace("_", " ");
			String isa = line[2];
			String ontology = line[3];
			String parent = line[4];
			ClassAtt ca = camap.get(ibclass);
			if (ca != null) {
				ca.feiISA = isa;
				ca.ontoFather = parent;
				ca.ontology = ontology;
			}
		}
		dr.close();
		// see the attributes distribution
		StrCmp sc = new StrCmp(true);
		Iterator<ClassAtt> it = camap.values().iterator();
		int howmanyAttMatched = 0;
		while (it.hasNext()) {
			ClassAtt ca = it.next();
			List<AttForIndex> candidate_att = new ArrayList<AttForIndex>();
			// look at what infobox can I find
			// if (ca.ontology != null) {
			// List<AttForIndex> temp = attributesIndex.get(ca.ontology
			// .toLowerCase());
			// if(temp!=null) candidate_att.addAll(temp);
			// // int size0 = candidate_att != null ? candidate_att.size() : 0;
			// // System.out.println(ca.ontology + "\t" + size0);
			// }
			if (ca.ontoFather != null) {
				List<AttForIndex> temp = attributesIndex.get(ca.ontoFather
						.toLowerCase());
				if (temp != null)
					candidate_att.addAll(temp);
				// int size0 = candidate_att != null ? candidate_att.size() : 0;
				// System.out.println(ca.ontoFather + "\t" + size0);
			}
			// System.out.println(ca.ibclass+"\t"+candidate_att.size());

			for (int i = 0; i < ca.attlist.size(); i++) {
				String attributeName = ca.attlist.get(i);
				List<String> matched = new ArrayList<String>();
				for (int j = 0; j < candidate_att.size(); j++) {
					String compareName = candidate_att.get(j).attribute;
					String ibclassfull = candidate_att.get(j).fullname;
					String ibclass = candidate_att.get(j).ibclass;
					String[] words = candidate_att.get(j).words;
					// check if words of the ib all apear in ca.ibclass
					boolean isParentNode = true;
					for (String a : words) {
						a = a.toLowerCase();
						if (!ca.ibclass.contains(a)
								&& !ca.ontology.toLowerCase().contains(a)
								&& !ca.ontoFather.toLowerCase().contains(a)) {
							isParentNode = false;
						}
					}
					if (!isParentNode)
						continue;
					double rat = sc.sharewordsRatio(attributeName, compareName);
					if (rat > 0.9) {
						String subib = ca.ibclass;
						String parentib = ibclass;
						if (!subib.toLowerCase().equals(parentib.toLowerCase())) {
							String a = ca.ibclass + "/" + attributeName + "\t"
									+ ibclassfull;
							matched.add(ibclassfull);
							System.out.println(a);
							osw.write(a + "\n");
						}
					}
				}
				if(matched.size()>0){
					howmanyAttMatched++;
				}
			}
			
			// String a = ca.ibclass + "\t" + ca.attlist.size() + "\t" +
			// ca.feiISA
			// + "\t" + ca.ontology + "\t" + ca.ontoFather + "\t"
			// + ca.attlist;
			// System.out.println(a.replace("null", ""));
		}
		System.out.println(howmanyAttMatched);
	}

	public static void indexAllAttribute() throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(
				"attributeNames.full.sort.uniq"));
		String aline;
		while ((aline = br.readLine()) != null) {
			aline = aline.trim();
			int start = aline.indexOf(" ");
			int size = Integer.parseInt(aline.substring(0, start));
			String att = aline.substring(start).replace("@", "/").trim();
			String temp[] = att.split("/");
			AttForIndex afi = new AttForIndex(att, temp[temp.length - 1]);
			afi.ibclass = temp[0];
			String candidate = StrCmp.removePunc(att.split("/")[0]
					.toLowerCase());
			String[] wordsInibclass = candidate.split(" ");
			afi.words = wordsInibclass;
			for (String a : wordsInibclass) {
				if (!attributesIndex.containsKey(a)) {
					List<AttForIndex> nlist = new ArrayList<AttForIndex>();
					nlist.add(afi);
					attributesIndex.put(a, nlist);
				} else {
					List<AttForIndex> nlist = attributesIndex.get(a);
					nlist.add(afi);
				}
			}
		}
		br.close();
		System.out.println(attributesIndex.size());
		System.out.println(attributesIndex.keySet());
	}

}
